package com.ruoyi.order.controller;

import com.alipay.api.AlipayApiException;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.utils.QRCodeUtil;
import com.ruoyi.order.utils.WxPayUtils;
import com.ruoyi.order.utils.ZhifubaoUtill;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 支付功能
 *
 * @author 12692
 */
@RestController
@RequestMapping("pay")
public class PayController {
    @Resource
    private ZhifubaoUtill zhifubaoUtill;

    @GetMapping("/zhifubaoPay/{orderId}/{orderMoney}/{orderCommodityName}")
    public String zhifubaoPay(@PathVariable String orderId, @PathVariable Integer orderMoney, @PathVariable String orderCommodityName) throws AlipayApiException {
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderId(orderId);
        tbOrder.setOrderMoney(new BigDecimal(orderMoney));
        String pay = zhifubaoUtill.pay(tbOrder);
        return pay;
    }

    @GetMapping("/zhifubaoResult/{orderId}")
    public String zhifubaoResult(@PathVariable String orderId) throws AlipayApiException {
        Boolean aBoolean = zhifubaoUtill.queryResult(orderId);
        if (aBoolean) {
            return "支付成功";
        }
        return "支付失败";
    }

    @Resource
    WxPayUtils wxPayUtils;

    @GetMapping("/wxPay/{orderId}")
    public void wxPay(@PathVariable String orderId, HttpServletResponse response) {
        String wxPay = wxPayUtils.wxPay(orderId);
        QRCodeUtil.createQRCode(response, wxPay);
    }

    @GetMapping("/wxResout/{orderId}")
    public String wxResour(@PathVariable String orderId) throws Exception {
        Boolean aBoolean = wxPayUtils.refreshWxPay(orderId);
        if (aBoolean) {
            TbOrder tbOrder = new TbOrder();
            tbOrder.setOrderId(orderId);
            tbOrder.setOrderStatus((long)6);
            return "支付成功";
        }
        return "支付失败";
    }
}
